package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.service.LogisticsService;
import com.cn.goodsadmin.service.OrderService;
import com.cn.goodsadmin.service.UserAddressService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    UserAddressService userAddressService;

    @Autowired
    LogisticsService logisticsService;

    /**
     * @param pageindex
     * @param pagesize
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/getOrderInfo", method = RequestMethod.GET, params = {"pageindex", "pagesize","appid","state"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getOrderInfo(int pageindex, int pagesize,int appid,int state) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            PageQueryResult<DestDTO> pageQueryResult = orderService.getAllOrderInfo(pageindex, pagesize,appid,state);
            List<DestDTO> destDTOS=pageQueryResult.getQueryResult();
            OrderPageInfo orderPageInfo=new OrderPageInfo();
            List<SendOrderTemplate>  templates=new ArrayList<SendOrderTemplate>();
            for(DestDTO destDTO:destDTOS){
                SendOrderTemplate sendOrderTemplate=new SendOrderTemplate();
                OrderInfo orderInfo=destDTO.getOrderinfo();
                sendOrderTemplate.orderInfo=orderInfo;
                int uaid=orderInfo.getcAddrid();
                Useraddress useraddress=userAddressService.findUserAddress(uaid);
                sendOrderTemplate.useraddress=useraddress;
                templates.add(sendOrderTemplate);
                String logid=orderInfo.getcLogisticsid();
                if(logid!=null) {
                    Logistics logistics = logisticsService.findLogisticsByLid(logid);
                    sendOrderTemplate.logistics=logistics;
                }
            }
            orderPageInfo.sendOrderTemplates=templates;
            orderPageInfo.pagecount=pageQueryResult.getPageCount();
            orderPageInfo.pageIndex=pageQueryResult.getPageIndex();
            orderPageInfo.pagesize=pageQueryResult.getPageSize();
            orderPageInfo.totalcount=pageQueryResult.getTotalCount();
            supplyMap.put("orderPageInfo",orderPageInfo);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/sendOrder", method = RequestMethod.GET,params = {"orderid","appid","logisticsid","logisticsname"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateOrderStutas(String orderid,int appid,String logisticsid,String logisticsname){
        SupplyMap supplyMap=new SupplyMap();
        try{
            orderService.sendOrder(orderid,appid,logisticsid,logisticsname);
        }catch (Exception e){
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
